package nl.mayisal.startup;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SQLConfig {

	private static String url;
	private static Properties info;
	private static String drivername;
	
	public static void initialize(String filename) {
		Properties config = new Properties();
		try {
			config.load(new FileInputStream(filename));
		} catch (Exception e) {
			config.setProperty("NameConnectionDriver","com.microsoft.sqlserver.jdbc.SQLServerDriver");
			config.setProperty("PrefixConnectionURL","jdbc:sqlserver://");
			config.setProperty("FileConnectionProperties","sqlconnector.properties");
			try {
				config.store(new FileOutputStream(filename),"");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		SQLConfig.url = config.getProperty("PrefixConnectionURL");
		SQLConfig.drivername = config.getProperty("NameConnectionDriver");
		final String connectionPropertiesFileName = config.getProperty("FileConnectionProperties");
		SQLConfig.info = new Properties();
		try {
			SQLConfig.info.load(new FileInputStream(connectionPropertiesFileName));
		} catch (Exception e) {
			SQLConfig.info.setProperty("applicationName","Analyser");
			SQLConfig.info.setProperty("databaseName","<Your databasename here>");
			SQLConfig.info.setProperty("serverName","<Your servername here>");
			SQLConfig.info.setProperty("portNumber","<Your portnumber here>");
			SQLConfig.info.setProperty("integratedSecurity","true");
			//		SQLConfig.info.setProperty("userName","");
			//		SQLConfig.info.setProperty("password",null);

			SQLConfig.info.setProperty("disableStatementPooling","true");
			SQLConfig.info.setProperty("encrypt","false");
			//		SQLConfig.info.setProperty("failoverPartner",null);
			//		SQLConfig.info.setProperty("hostnameInCertificate",null);
			SQLConfig.info.setProperty("instanceName","<Your instancename here>");
			SQLConfig.info.setProperty("lastUpdateCount","true");
			SQLConfig.info.setProperty("lockTimeout","-1");
			SQLConfig.info.setProperty("loginTimeout","15");
			SQLConfig.info.setProperty("packetSize","8000");
			SQLConfig.info.setProperty("responseBuffering","adaptive");
			SQLConfig.info.setProperty("selectMethod","direct");
			SQLConfig.info.setProperty("sendStringParametersAsUnicode","true");
			SQLConfig.info.setProperty("trustServerCertificate","false");
			//		SQLConfig.info.setProperty("trustStore",null);
			//		SQLConfig.info.setProperty("trustStorePassword",null);
			SQLConfig.info.setProperty("workstationID","");
			SQLConfig.info.setProperty("xopenStates","false");
			try {
				SQLConfig.info.store(new FileOutputStream(connectionPropertiesFileName), "Settings for jdbc mssql driver 2.0");
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} 
	}
	
	public static String getUrl() {
		return SQLConfig.url;
	}

	public static Properties getConnectionProperties() {
		return SQLConfig.info;
	}

	public static String getDriverName() {
		return SQLConfig.drivername;
	}

}
